﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConsoleApp_e1e2;

namespace WindowsFormsApp_f11
{
    public partial class Form1 : Form
    {
        OrderService ser = new OrderService();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 fm = new Form2();
            fm.ShowDialog();
            OrderDetails detai = new OrderDetails(fm.date, fm.time, fm.num);
            Order ord = new Order(fm.id, fm.commodity, fm.client, fm.money, detai);
            if (ser.add(ord)) textBox1.Text = "添加成功";
            else textBox1.Text = "已有重复订单，添加失败";
            orderBindingSource.DataSource = null;
            orderBindingSource.DataSource = ser.list;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "") textBox1.Text = "输入为空，请输入要查询的客户名称";
            else
            {
                if (ser.search3(textBox2.Text))
                {
                    orderBindingSource.DataSource = ser.list1;
                    textBox1.Text = "查询成功";
                }
                else textBox1.Text = "查询失败";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "") textBox1.Text = "输入为空，请输入要删除的订单号";
            else
            {
                int id1 = Convert.ToInt32(textBox2.Text);
                if (ser.remove(id1)) textBox1.Text = "删除成功";
                else textBox1.Text = "未找到订单，删除失败";
            }
            orderBindingSource.DataSource = null;
            orderBindingSource.DataSource = ser.list;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form2 fm = new Form2();
            fm.ShowDialog();
            OrderDetails detai = new OrderDetails(fm.date, fm.time, fm.num);
            Order ord = new Order(fm.id, fm.commodity, fm.client, fm.money, detai);
            if (ser.modify(ord)) textBox1.Text = "修改成功";
            else textBox1.Text = "未找到订单号，修改失败";
            orderBindingSource.DataSource = null;
            orderBindingSource.DataSource = ser.list;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            orderBindingSource.DataSource = null;
            orderBindingSource.DataSource = ser.list;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "") textBox1.Text = "输入为空，请输入要查询的订单号";
            else
            {
                int id1 = Convert.ToInt32(textBox2.Text);
                if (ser.search1(id1))
                {
                    orderBindingSource.DataSource = null;
                    orderBindingSource.DataSource = ser.list1;
                    textBox1.Text = "查询成功";
                }
                else textBox1.Text = "查询失败";
            }
        }
        private void button5_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "") textBox1.Text = "输入为空，请输入要查询的商品名称";
            else
            {
                if (ser.search2(textBox2.Text))
                {
                    orderBindingSource.DataSource = null;
                    orderBindingSource.DataSource = ser.list1;
                    textBox1.Text = "查询成功";
                }
                else textBox1.Text = "查询失败";
            }
        }
        private void button9_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "") textBox1.Text = "输入为空，请输入要查询的金额";
            else
            {
                int mon = Convert.ToInt32(textBox2.Text);
                if (ser.search4(mon))
                {
                    orderBindingSource.DataSource = null;
                    orderBindingSource.DataSource = ser.list1;
                    textBox1.Text = "查询成功";
                }
                else textBox1.Text = "查询失败";
            }
        }
        private void button7_Click(object sender, EventArgs e)
        {
            ser.sort1();
            orderBindingSource.DataSource = ser.list;
        }
        private void button8_Click(object sender, EventArgs e)
        {
            ser.sort3();
            orderBindingSource.DataSource = ser.list;
        }
        private void button11_Click(object sender, EventArgs e)
        {
            ser.Import(textBox2.Text);
            orderBindingSource.DataSource = ser.list;
            textBox1.Text = "导入成功";
        }
        private void button12_Click(object sender, EventArgs e)
        {
            ser.Export(textBox2.Text,ser.list);
            textBox1.Text = "导出成功";
        }
    }
}
